package com.fingard.dsp.bank.directbank.boc01;

import java.util.*;

import com.fingard.constant.Format;
import com.fingard.dsp.bank.directbank.bankInterface.IBalanceCheckConfirm;
import com.fingard.dsp.bank.reqrespobj.balancecheckconfirm.ReqBalCheckConfirmQry;
import com.fingard.dsp.bank.reqrespobj.balancecheckconfirm.RetAtsBalCheckConfirmQry;
import com.fingard.dsp.bank.reqrespobj.balancecheckconfirm.detail.RetAtsBalCheckConfirmDetail;
import com.fingard.text.StringHelper;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

/**
 * Created by Lewis on 2017/6/6.
 */
public class BOC01BalanceCheckConfirm extends BOC01Base implements IBalanceCheckConfirm {
    @Override
    public RetAtsBalCheckConfirmQry d_7509_BalCheckConfirm(ReqBalCheckConfirmQry p_req7509) throws Exception {
        //余额对账查询返回ATS的对象
        RetAtsBalCheckConfirmQry retAts = new RetAtsBalCheckConfirmQry(p_req7509.reqHeader);
        retAts.writeStart();//写返回ATS的统一头部信息
        try {
            BOC01Header bankHeader = new BOC01Header();
            bankHeader.trncod = "b2e0360";//账单核对
            bankHeader.trnid = getQryTransID();//报文编号 字母数字串 0-12位
            XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
            //写入通用的头部节点
            writeCommonHeader(xmlWriter, bankHeader);
            xmlWriter.writeStartElement("trn-b2e0360-rq");
            xmlWriter.writeStartElement("b2e0360-rq");
            int count = 0;
            Map<Integer,Map<String,String>> mapList = new HashMap<Integer, Map<String, String>>();
            while (p_req7509.readRecord()){
                count++;
                Map<String,String> map = new HashMap<String, String>();
                String statementNumber = p_req7509.readedDetail.statementNumber;
                String dzFlag = p_req7509.readedDetail.dzFlag;
                String actNum = p_req7509.readedDetail.actNum;
                String custActBal = p_req7509.readedDetail.custActBal;
                String custAmountIncurred = p_req7509.readedDetail.custAmountIncurred;
                String notConfirmReason = p_req7509.readedDetail.notConfirmReason;
                map.put("statementNumber",statementNumber);
                map.put("dzFlag",dzFlag);
                map.put("actNum",actNum);
                map.put("custActBal",custActBal);
                map.put("custAmountIncurred",custAmountIncurred);
                map.put("notConfirmReason",notConfirmReason);
                mapList.put(count,map);
            }
           /* xmlWriter.writeElementString("stmtnum", p_req7509.readedDetail.statementNumber);*///这两取得到值么 在while的外部？
            xmlWriter.writeElementString("stmtnum", mapList.get(1).get("statementNumber"));
            String dzFlag = mapList.get(1).get("dzFlag");
            if("1".equals(dzFlag)||"3".equals(dzFlag)){//未对账或不平
                dzFlag = "0";
            }
            else if("2".equals(dzFlag)){
                dzFlag = "1";
            }
            xmlWriter.writeElementString("isrestore", dzFlag);//（0为余额不符 1为余额相符）
            xmlWriter.writeElementString("insid",bankFront.org);
            xmlWriter.writeStartElement("multidata");
            p_req7509.readRecord();
            for(int i=0;i<p_req7509.detailCount;i++){
                xmlWriter.writeStartElement("confirmdata");//明细内容
                xmlWriter.writeElementString("actacn", mapList.get(i+1).get("actNum"));
                xmlWriter.writeElementString("bookno", "");//册号
                xmlWriter.writeElementString("seqno", String.valueOf(i+1));//序号
                xmlWriter.writeElementString("bokbal",mapList.get(i+1).get("custActBal"));//客户账面余额（余额不符时为必填项）
                xmlWriter.writeElementString("vchnum", p_req7509.reqHeader.reqSeqID);//核对号（余额不符时为必填项）
                xmlWriter.writeElementString("amount", mapList.get(i+1).get("custAmountIncurred"));//发生额（余额不符时为必填项）
                xmlWriter.writeElementString("moddate", Format.Date8Format.format(new Date()));
                xmlWriter.writeElementString("memo", "");
                //1. 客户已收账,银行未收账2. 客户已付账,银行未付账3. 银行已收账,客户未收账4. 银行已付账,客户未付账当标签
                xmlWriter.writeElementString("opreason",mapList.get(i+1).get("notConfirmReason"));//不符原因（余额不符时为必填项）
                xmlWriter.writeEndElement();
            }
            xmlWriter.writeEndElement();
            xmlWriter.writeEndElement();
            xmlWriter.writeEndElement();//trn-b2e0360-rq
            xmlWriter.writeEndDocument();
            String[] tmpStrRet = sendToBank(xmlWriter.toXmlString());
            //模拟银行返回
            /*tmpStrRet[0]="";
            tmpStrRet[1]="<bocb2e>" +
                    "<trans>" +
                    "<trn-b2e0360-rs>" +
                    "<status>" +
                    "<rspcod>B001</rspcod>" +
                    "<rspmsg>成功</rspmsg>" +
                    "</status>" +
                    "<b2e0360-rs>" +
                    "<status>" +
                    "<rspcod>B001</rspcod>" +
                    "<rspmsg>查询成功</rspmsg>" +
                    "</status>" +
                    "<stmtnum>"+p_req7509.reqHeader.transSeqID+"</stmtnum>" +
                    "</b2e0360-rs>" +
                    "</trn-b2e0360-rs>" +
                    "</trans>" +
                    "</bocb2e>";*/
            if(tmpStrRet[0].length() == 0){
                StringBuilder tmpSbMsg = new StringBuilder();

                XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);

                if(tmpBkReader != null){
                    String tmprspcod = tmpBkReader.getFirstText("/bocb2e/trans/trn-b2e0360-rs/status/rspcod");
                    String tmprspmsg = tmpBkReader.getFirstText("/bocb2e/trans/trn-b2e0360-rs/status/rspmsg");
                    if(isActiveRetCode(tmprspcod)){
                        while(tmpBkReader.read("/bocb2e/trans/trn-b2e0360-rs/b2e0360-rs")){
                            RetAtsBalCheckConfirmDetail tmpRetDetail = new RetAtsBalCheckConfirmDetail();
                            if(tmpBkReader.readedXElement.getSingleChild("status") != null){
                                String tmpEachRspcod = tmpBkReader.readedXElement.getSingleChild("status").getSingleText("rspcod");
                                String tmpEachRspmsg = tmpBkReader.readedXElement.getSingleChild("status").getSingleText("rspmsg");
                                if (tmpEachRspcod.equals("B001"))//这个状态不知道有没有什么标志
                                {
                                    tmpRetDetail.respCode = "0";
                                    tmpRetDetail.respInfo = tmpEachRspmsg;
                                    tmpRetDetail.statementNumber = tmpBkReader.readedXElement.getSingleText("stmtnum");
                                    tmpRetDetail.result = "2";
                                }
                                else{
                                    tmpRetDetail.respCode = "-1";
                                    tmpRetDetail.respInfo = tmpEachRspmsg;
                                    if(StringHelper.isNullOrEmpty(tmpRetDetail.respInfo)){
                                        tmpRetDetail.respInfo = tmpEachRspcod;
                                    }
                                }
                            }
                            else
                            {
                                tmpRetDetail.respCode = "-1";
                                tmpRetDetail.respInfo = "status节点为空";
                            }
                            retAts.writeRetDetail(tmpRetDetail, p_req7509.isBatch);
                        }
                    }
                    else{
                        retAts.writeRespCode("-1");
                        retAts.writeRespInfo(tmprspmsg);
                    }
                }
                else{
                    retAts.writeRespCode("-1");
                    retAts.writeRespInfo(tmpSbMsg.toString());
                }
            }
            else{
                retAts.writeRespCode("-1");
                retAts.writeRespInfo(tmpStrRet[1]);
            }
        } catch (Exception e) {
            retAts.writeRespCode("-1");
            retAts.writeRespInfo(e.getMessage());
            e.printStackTrace();
        }
        retAts.writeEnd();
        return retAts;
    }
}
